package pro.shushi.pamirs.demo.core.init;

import org.apache.rocketmq.acl.common.SessionCredentials;
import org.apache.rocketmq.client.AccessChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.boot.common.api.command.AppLifecycleCommand;
import pro.shushi.pamirs.boot.common.api.init.InstallDataInit;
import pro.shushi.pamirs.boot.common.api.init.UpgradeDataInit;
import pro.shushi.pamirs.demo.api.DemoModule;
import pro.shushi.pamirs.demo.core.mq.DemoItemMQProducer;
import pro.shushi.pamirs.demo.core.mq.DemoItemMessageDemoItemListener;
import pro.shushi.pamirs.framework.connectors.event.api.NotifyConsumer;
import pro.shushi.pamirs.framework.connectors.event.engine.EventEngine;
import pro.shushi.pamirs.framework.connectors.event.enumeration.ConsumerType;
import pro.shushi.pamirs.framework.connectors.event.enumeration.NotifyType;
import pro.shushi.pamirs.framework.connectors.event.rocketmq.RocketMQEventPushConsumer;
import pro.shushi.pamirs.framework.connectors.event.rocketmq.aliyun.AliyunExtendProperty;
import pro.shushi.pamirs.framework.connectors.event.rocketmq.aliyun.AliyunRPCHook;
import pro.shushi.pamirs.framework.connectors.event.rocketmq.config.RocketMQNotifyConfiguration;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.common.spring.BeanDefinitionUtils;
import pro.shushi.pamirs.trigger.service.TriggerTaskActionService;
import pro.shushi.pamirs.workflow.app.api.constant.WorkflowConstant;

import java.util.Collections;
import java.util.List;

/**
 * 权限模块的数据初始化
 *
 * @author shier
 * date 2020/4/16
 */
@Slf4j
@Component
@Order(0)
public class DemoAppDataInit implements InstallDataInit, UpgradeDataInit {

    @Autowired
    private TriggerTaskActionService triggerTaskActionService;

    @Autowired
    private RocketMQNotifyConfiguration rocketMQNotifyConfiguration;

    @Override
    public boolean init(AppLifecycleCommand command, String version) {
        mqConsumerInit();
        return true;
    }

    @Override
    public boolean upgrade(AppLifecycleCommand command, String version, String existVersion) {
        mqConsumerInit();
        return true;
    }

    private void mqConsumerInit(){
        final String consumerGroup = DemoItemMQProducer.PAMIRS_MESSAGE_DEMO_ITEM_TOPIC;
        NotifyConsumer consumer = EventEngine.registerConsumer(NotifyType.ROCKET_MQ, consumerGroup, () -> {
            RocketMQEventPushConsumer.Builder builder = RocketMQEventPushConsumer.newInstance(rocketMQNotifyConfiguration.getNamesrvAddr(), consumerGroup)
                    .setOriginClass(String.class)
                    .setClientIP(rocketMQNotifyConfiguration.getClientIp())
                    .setInstanceName(rocketMQNotifyConfiguration.getInstanceName())
                    .setNamespace(rocketMQNotifyConfiguration.getNamespace())
                    .setConsumerType(ConsumerType.CONCURRENTLY);
                    if (rocketMQNotifyConfiguration.getAliyun().getEnabled()) {
                        builder.setAccessChannel(AccessChannel.CLOUD)
                                .setRpcHook(new AliyunRPCHook(new SessionCredentials(rocketMQNotifyConfiguration.getAliyun().getAccessKey(), rocketMQNotifyConfiguration.getAliyun().getSecretKey()), new AliyunExtendProperty()))
                                .setInstanceName(rocketMQNotifyConfiguration.getAliyun().getInstanceId())
                                .setGroup(rocketMQNotifyConfiguration.getAliyun().getGroupId());
                    }
                    return builder.build();

        });
        if (consumer != null) {
            consumer.subscribe(WorkflowConstant.PAMIRS_MESSAGE_TRIGGER_TOPIC, "*");
            //此处对未注册到Bean中的消费监听不进行判断，拿不到指定Bean或Bean重复的时候直接抛Spring指定异常
            consumer.registerListener(BeanDefinitionUtils.getBean(DemoItemMessageDemoItemListener.class));
        }
    }

    @Override
    public List<String> modules() {
        return Collections.singletonList(DemoModule.MODULE_MODULE);
    }

    @Override
    public int priority() {
        return 0;
    }
}
